package com.msconfig.mvp.view.fragment;

import android.app.DialogFragment;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.TextView;

import com.geo.msconfig.R;
import com.msconfig.widget.RoundRectProgressView;


public class ConnectioningDialogFragment extends DialogFragment implements View.OnClickListener {
    private static ConnectioningDialogFragment singleton;
    private View.OnClickListener mListener;
    private RoundRectProgressView roundRectProgressView;
    private TextView tvStatus;


//    public static ConnectioningDialogFragment newInstance() {
//        Bundle args = new Bundle();
//        ConnectioningDialogFragment fragment = new ConnectioningDialogFragment();
//        fragment.setArguments(args);
//        return fragment;
//    }

    //单例模式
    public static ConnectioningDialogFragment getSingleton() {
        if (singleton == null) {  //第一次检查，避免不必要的同步
            synchronized (ConnectioningDialogFragment.class) {  //同步
                if (singleton == null) {   //第二次检查，为null时才创建实例
                    singleton = new ConnectioningDialogFragment();
                }
            }
        }
        return singleton;
    }


    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.layout_activity_connectioning_dialog, container, false);
        roundRectProgressView=view.findViewById(R.id.rp_progress);
        tvStatus=view.findViewById(R.id.tv_status);
        roundRectProgressView.setMaxCount(100);
        setProgress(0,"正在连接设备......");
        view.findViewById(R.id.tv_cancel).setOnClickListener(this);
        //设置dialog没有title
        getDialog().requestWindowFeature(Window.FEATURE_NO_TITLE);
        //背景
        getDialog().getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        //无边框
        setStyle(android.support.v4.app.DialogFragment.STYLE_NO_FRAME, android.R.style.Theme);

        return view;
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.tv_cancel:
                //回调出去给外面使用
                if (mListener != null) {
                    mListener.onClick(view);
                }
                break;
        }
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        //注意下面这个方法会将布局的根部局忽略掉，所以需要嵌套一个布局
        Window dialogWindow = getDialog().getWindow();
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();
        lp.gravity = Gravity.CENTER;
        //改变在屏幕中的位置,如果需要改变上下左右具体的位置，比如100dp，则需要对布局设置margin
        //设置对话框宽度高度
//        Display defaultDisplay = getActivity().getWindowManager().getDefaultDisplay();
//        lp.width = 200;  //改变宽度
//        lp.height=300;//   改变高度
        dialogWindow.setAttributes(lp);

        getDialog().setCancelable(false);
        //点击对话框外面退出对话框
        getDialog().setCanceledOnTouchOutside(false);
        //可以在这拦截返回键啊home键啊事件
        getDialog().setOnKeyListener(new DialogInterface.OnKeyListener() {
            @Override
            public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
                //dialog.dismiss();
                return true;
            }
        });
    }

    public void setClickListener(View.OnClickListener listener) {
        this.mListener = listener;
    }

    public void setProgress(int progress,String content){
        roundRectProgressView.setProgress(progress);
        tvStatus.setText(content);
    }


}
